import numpy as np
import matplotlib.pyplot as plt
import pandas as pd



def x():
    train_Loss = []
    drivable_mIoU = []
    lane_mIoU = []
    accuracy = []

    for log_file in log_file_list:
        for line in open(log_file,"r",encoding='UTF-8'):
            if line[24:29] == 'head:':
                line = line[29:]
                drivable_mIoU.append(float(line.split(', ')[3])*100)
                lane_mIoU.append(float(line.split(', ')[-4])*100)
                accuracy.append(float(line.split(', ')[-2])*100)
                train_Loss.append(float(line.split(', ')[-1]))

    x = [i+1 for i in range(len(drivable_mIoU))]
    print(max(train_Loss), )
    print(max(drivable_mIoU))
    print(max(lane_mIoU))
    print(max(accuracy))

    fig,ax = plt.subplots(figsize=(12,9))
    ax.plot(x, train_Loss, color='black', linewidth=2, marker='o', label='train_loss')
    ax.set_xlabel('epoch', fontsize=20)
    ax.set_ylabel('loss', color='black', fontsize=20)


    plt.grid(True)
    ax2 = ax.twinx()
    ax2.plot(x, drivable_mIoU, color='red', linewidth=2, marker='^', label='drivable_miou')
    ax2.plot(x, lane_mIoU, color='green', linewidth=2, marker='s', label='lane_miou')
    ax2.plot(x, accuracy, color='blue', linewidth=2, marker='d', label='scenes_acc')
    ax2.set_ylabel('mIoU', color='red', fontsize=20)
    plt.title("RLSnet",fontsize=20)
    plt.legend()
    plt.show()
    plt.savefig(log_file_list[0].replace('.log', '.png'))
    
    
def processing(csv1: str, csv2: str):
    fig,ax = plt.subplots(figsize=(12,9))
    
    ax.set_xlabel('epoch', fontsize=15)
    ax.set_ylabel('learning rate', color='red', fontsize=15)
    plt.grid(True)
    ax2 = ax.twinx()
    ax2.set_ylabel('seg loss', color='green', fontsize=15)
    data1 = pd.read_csv(csv1)
    data2 = pd.read_csv(csv2)
    step = data1.loc[:, "Step"]
    value1 = data1.loc[:, "Value"]
    value2 = data2.loc[:, "Value"]
    step = step.to_list()
    value1 = value1.to_list()
    value2 = value2.to_list()
    
    ax2.plot(step, value2, color='green', linewidth=1, marker='.', label='seg_loss')
    
    ax.plot(step, value1, color='red', linewidth=1, ls='-.', marker='.', label='LR_decay')
    
    plt.legend()
    plt.title("ALIKE With SegHead",fontsize=15)
    
    # plt.set_xlabel('epoch', fontsize=20)
    plt.show()

if __name__ == "__main__":
    processing("/Projects/ALIKE/csvdata/1021/learning_rate.csv", "/Projects/ALIKE/csvdata/1021/train_loss.csv")
    
    